<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>嵌套(nested)查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl-nested-query.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-nested-query.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-nested-query.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl-nested-query.html" rel="nofollow" target="_blank">../en/query-dsl-nested-query.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="query-dsl.html">查询领域特定语言(Query DSL)</a></span>
»
<span class="breadcrumb-link"><a href="joining-queries.html">联结查询</a></span>
»
<span class="breadcrumb-node">嵌套(nested)查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="joining-queries.html">« 联结查询</a>
</span>
<span class="next">
<a href="query-dsl-has-child-query.html">Has child query »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-nested-query"></a>嵌套(nested)查询
</h2>
</div></div></div>

<p>包裹另一个查询以搜索<a class="xref" href="nested.html" title="Nested datatype">nested(嵌套)</a>字段。</p>
<p>
<code class="literal">nested</code>查询搜索嵌套的字段对象，就好像它们被索引为单独的文档一样。  

如果对象与搜索匹配，<code class="literal">nested</code>查询将返回根父文档。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="nested-query-ex-request"></a>请求示例
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="nested-query-index-setup"></a>准备一个索引
</h4>
</div></div></div>
<p>若要使用<code class="literal">nested</code>查询，索引必须包含<a class="xref" href="nested.html" title="Nested datatype">nested</a>字段映射。例如：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my_index
{
    "mappings" : {
        "properties" : {
            "obj1" : {
                "type" : "nested"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/200.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="nested-query-ex-query"></a>查询示例
</h4>
</div></div></div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_index/_search
{
    "query":  {
        "nested" : {
            "path" : "obj1",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"obj1.name" : "blue"} },
                    { "range" : {"obj1.count" : {"gt" : 5}} }
                    ]
                }
            },
            "score_mode" : "avg"
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/201.console"></div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="nested-top-level-params"></a><code class="literal">nested</code>的顶级参数
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">path</code>
</span>
</dt>
<dd>
(必需, string) 要搜索的嵌套对象的路径。
</dd>
<dt>
<span class="term">
<code class="literal">query</code>
</span>
</dt>
<dd>
<p>(必需, query object) 要在指定的<code class="literal">path</code>中的嵌套对象上运行的查询。 
如果一个对象与搜索匹配，<code class="literal">nested</code>查询将返回根父文档。
</p>
<p>可以使用包含完整路径的点符号来搜索嵌套字段，例如<code class="literal">obj1.name</code>。</p>
<p>
自动支持和检测多级嵌套，导致内部嵌套查询自动匹配相关的嵌套层级，而不是根(如果它存在于另一个嵌套查询中)。-- TODO 待根据后面的内容修正

Multi-level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level, rather than root, if it exists within another nested query.
</p>
<p>有关示例请参考 <a class="xref" href="query-dsl-nested-query.html#multi-level-nested-query-ex" title="Multi-level nested queries">多级嵌套查询</a>。</p>
</dd>
<dt>
<span class="term">
<code class="literal">score_mode</code>
</span>
</dt>
<dd>
<p>(可选, string) 指示匹配子对象的分数如何影响根父文档的<a class="xref" href="query-filter-context.html#relevance-scores" title="Relevance scores">相关性评分</a>。有效值有：</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">avg</code> (默认)
</span>
</dt>
<dd>
使用所有匹配子对象的相关性评分的平均分。
</dd>
<dt>
<span class="term">
<code class="literal">max</code>
</span>
</dt>
<dd>
使用所有匹配子对象的相关性评分的最高分。
</dd>
<dt>
<span class="term">
<code class="literal">min</code>
</span>
</dt>
<dd>
使用所有匹配子对象的相关性评分的最低分。
</dd>
<dt>
<span class="term">
<code class="literal">none</code>
</span>
</dt>
<dd>
不使用匹配子对象的相关性评分。该查询为父文档分配的分数为 <code class="literal">0</code>。
</dd>
<dt>
<span class="term">
<code class="literal">sum</code>
</span>
</dt>
<dd>
将所有匹配子对象的相关性评分相加。
</dd>
</dl>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">ignore_unmapped</code>
</span>
</dt>
<dd>
<p>(可选, boolean) 指示是否忽略未映射的<code class="literal">path</code>并且不返回任何文档而不是错误。默认为 <code class="literal">false</code>。</p>
<p>如果设置为<code class="literal">false</code>，则当<code class="literal">path</code>是未映射的字段时，Elasticsearch 会抛出错误。</p>
<p>可以使用此参数来查询可能不包含<code class="literal">path</code>路径的多个索引。</p>
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="nested-query-notes"></a>注意
</h3>
</div></div></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="multi-level-nested-query-ex"></a>多级嵌套查询 (multi-level nested queries)
</h4>
</div></div></div>
<p>
要了解多级嵌套查询是如何工作的，首先你需要一个包含嵌套字段的索引。

下面这个请求定义了具有嵌套字段<code class="literal">make</code>和<code class="literal">model</code>的名为<code class="literal">drivers</code>的索引的映射。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /drivers
{
    "mappings" : {
        "properties" : {
            "driver" : {
                "type" : "nested",
                "properties" : {
                    "last_name" : {
                        "type" : "text"
                    },
                    "vehicle" : {
                        "type" : "nested",
                        "properties" : {
                            "make" : {
                                "type" : "text"
                            },
                            "model" : {
                                "type" : "text"
                            }
                        }
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/202.console"></div>
<p>接下来，添加并索引几个文档到索引<code class="literal">drivers</code>中：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">
# 添加第1个文档，指定id为1
PUT /drivers/_doc/1
{
  "driver" : {
        "last_name" : "McQueen",
        "vehicle" : [
            {
                "make" : "Powell Motors",
                "model" : "Canyonero"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    }
}

# 添加第2个文档，指定id为2
PUT /drivers/_doc/2?refresh
{
  "driver" : {
        "last_name" : "Hudson",
        "vehicle" : [
            {
                "make" : "Mifune",
                "model" : "Mach Five"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/203.console"></div>
<p>现在，你可以使用多级嵌套查询来根据<code class="literal">make</code>和<code class="literal">model</code>字段匹配文档：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /drivers/_search
{
    "query" : {
        "nested" : {
            "path" : "driver",
            "query" : {
                "nested" : {
                    "path" :  "driver.vehicle",
                    "query" :  {
                        "bool" : {
                            "must" : [
                                { "match" : { "driver.vehicle.make" : "Powell Motors" } },
                                { "match" : { "driver.vehicle.model" : "Canyonero" } }
                            ]
                        }
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/204.console"></div>
<p>搜索请求返回如下响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.7349272,
    "hits" : [
      {
        "_index" : "drivers",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 3.7349272,
        "_source" : {
          "driver" : {
            "last_name" : "McQueen",
            "vehicle" : [
              {
                "make" : "Powell Motors",
                "model" : "Canyonero"
              },
              {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
              }
            ]
          }
        }
      }
    ]
  }
}</pre>
</div>
</div>

</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="joining-queries.html">« 联结查询</a>
</span>
<span class="next">
<a href="query-dsl-has-child-query.html">has_child查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>